package vn.edu.uit.chemistry;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Map;

import vn.edu.uit.chemistry.NopBaiCu.UpdatePrecentAsyncTask;
import vn.edu.uit.chemistry.model.FakeGuiTaiLieuAdapter;
import vn.edu.uit.chemistry.utilities.TCPClient;
import vn.edu.uit.chemistry.view.ViewGuiTaiLieu;
import vn.edu.uit.chemistry.view.ViewRowNopBaiCu;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Shader.TileMode;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class GuiTaiLieu extends Activity {

	TextView tvTitle;
	ImageView imgGuiFile;
	TextView tvFile;
	TextView tvSize;

	public static LinearLayout lvGuiTL;
	String path = "";
	String title = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_gui_tai_lieu);

		Intent startIntent = getIntent();
		if (startIntent != null) {
			Bundle b = startIntent.getExtras();
			path = b.getString("File");
			title = b.getString("Title");
		}

		// Toast.makeText(getApplicationContext(), path + " - " + title,
		// Toast.LENGTH_LONG).show();
		Log.d("vuxuan", path + "-" + title);

		File myfile = new File(path);

		tvTitle = (TextView) findViewById(R.id.tvTitleGui);
		if (title.equals("TLGV")) {
			tvTitle.setText("Gửi tài liệu cho học sinh");
		} else if (title.equals("KT15")) {
			tvTitle.setText("Gửi bài kiểm tra 15 phút");
		} else if (title.equals("DACT")) {
			tvTitle.setText("Gửi đáp án chi tiết");
		} else if (title.equals("BC")) {
			tvTitle.setText("Gửi đáp án bài cũ");
		} else if (title.equals("BK")) {
			tvTitle.setText("Gửi tài liệu cho học sinh");
		}
		imgGuiFile = (ImageView) findViewById(R.id.imgGuiFile);
		tvFile = (TextView) findViewById(R.id.tvFile);
		tvFile.setText(myfile.getName());
		tvSize = (TextView) findViewById(R.id.tvSize);
		tvSize.setText("" + myfile.length() + " bytes");

		lvGuiTL = (LinearLayout) findViewById(R.id.listGuiTL);
		imgGuiFile.postDelayed(onEverySecond, 1);
	}

	@Override
	protected void onResume() {
		super.onResume();
		//new SendAsyncTask().execute();
		sendFile();
	}
	
	public void sendFile() {
		SharedPreferences prefs = getSharedPreferences("DSGui", 0);
		Map<String, ?> keys = prefs.getAll();
		for (Map.Entry<String, ?> entry : keys.entrySet()) {
			String key = entry.getKey();
			String value = prefs.getString(entry.getKey(), "mshs#tenHS#0");
			
			new SendAsyncTask(key, value).execute();
		}
	}

//	final class SendAsyncTask extends AsyncTask<Void, Void, Void> {
//
//		public SendAsyncTask() {
//			//
//		}
//
//		@Override
//		protected Void doInBackground(Void... params) {
//			SharedPreferences prefs = getSharedPreferences("DSGui", 0);
//			Map<String, ?> keys = prefs.getAll();
//			for (Map.Entry<String, ?> entry : keys.entrySet()) {
//				SharedPreferences.Editor editor = prefs.edit();
//				String value = prefs.getString(entry.getKey(), "mshs#tenHS#0");
//				try {
//					if (title.equals("TLGV")) {
//						TCPClient client = new TCPClient(
//								getApplicationContext(), entry.getKey(), 54321);
//						client.guiTaiLieu(path);
//					} else if (title.equals("KT15")) {
//						prefs = getSharedPreferences("DSLamBai", 0);
//						editor = prefs.edit();
//						editor.putString(entry.getKey(), value + "#0");
//						editor.commit();
//						TCPClient client = new TCPClient(
//								getApplicationContext(), entry.getKey(), 54321);
//						client.guiBaiKT15(path);
//					} else if (title.equals("DACT")) {
//						TCPClient client = new TCPClient(
//								getApplicationContext(), entry.getKey(), 54321);
//						client.guiDapAnChiTietKT15(path);
//					} else if (title.equals("BC")) {
//						TCPClient client = new TCPClient(
//								getApplicationContext(), entry.getKey(), 54321);
//						client.guiDapAnBaiCu(path);
//					} else if (title.equals("BK")) {
//						TCPClient client = new TCPClient(
//								getApplicationContext(), entry.getKey(), 54321);
//						client.guiFileBatKy(path);
//					}
//
//				} catch (UnknownHostException e) {
//					e.printStackTrace();
//				} catch (IOException e) {
//					e.printStackTrace();
//				} catch (ClassNotFoundException e) {
//					e.printStackTrace();
//				}
//			}
//			return null;
//		}
//
//		@Override
//		protected void onPostExecute(final Void unused) {
//
//		}
//
//	}
	
	final class SendAsyncTask extends AsyncTask<Void, Void, Void> {

		Context ctx;
		String key = "";
		String value = "";
		
		public SendAsyncTask(String _key, String _value) {
			ctx = GuiTaiLieu.this;
			this.key = _key;
			this.value = _value;
		}

		@Override
		protected Void doInBackground(Void... params) {
				try {
					if (title.equals("TLGV")) {
						TCPClient client = new TCPClient(
								ctx, key, 54321);
						client.guiTaiLieu(path);
					} else if (title.equals("KT15")) {
						SharedPreferences prefs = getSharedPreferences("DSLamBai", 0);
						SharedPreferences.Editor editor = prefs.edit();
						editor.putString(key, value + "#0");
						editor.commit();
						TCPClient client = new TCPClient(
								ctx, key, 54321);
						client.guiBaiKT15(path);
					} else if (title.equals("DACT")) {
						TCPClient client = new TCPClient(
								ctx, key, 54321);
						client.guiDapAnChiTietKT15(path);
					} else if (title.equals("BC")) {
						TCPClient client = new TCPClient(
								ctx, key, 54321);
						client.guiDapAnBaiCu(path);
					} else if (title.equals("BK")) {
						TCPClient client = new TCPClient(
								ctx, key, 54321);
						client.guiFileBatKy(path);
					}

				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			return null;
		}

		@Override
		protected void onPostExecute(final Void unused) {

		}

	}

	public Runnable onEverySecond = new Runnable() {
		public void run() {
			new UpdatePrecentAsyncTask(getApplicationContext()).execute();
		}
	};

	final class UpdatePrecentAsyncTask extends AsyncTask<Void, Void, Void> {

		private Context context;
		private ViewGuiTaiLieu viewGuiTaiLieu;
		private LayoutParams lp;
		private Map<String, ?> keys;

		public UpdatePrecentAsyncTask(Context ctx) {
			this.context = ctx;
		}

		@Override
		protected Void doInBackground(Void... param) {
			SharedPreferences prefs = context.getSharedPreferences("DSGui", 0);
			keys = prefs.getAll();

			return null;
		}

		@Override
		protected void onPostExecute(final Void unused) {
			lvGuiTL.removeAllViews();
			for (Map.Entry<String, ?> entry : keys.entrySet()) {
				String[] values = entry.getValue().toString().split("#");
				viewGuiTaiLieu = new ViewGuiTaiLieu(context);
				viewGuiTaiLieu.setContent(values[1], values[0],
						Integer.parseInt(values[2]));
				lvGuiTL.addView(viewGuiTaiLieu.layout);
			}

			imgGuiFile.postDelayed(onEverySecond, 1);
		}

	}
}
